package com.example.myone.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myone.Activity.ArticleActivity;
import com.example.myone.Activity.AuthorActivity;
import com.example.myone.Entity.Article;
import com.example.myone.Entity.User;
import com.example.myone.Mapper.UserMapper;
import com.example.myone.Pager.HomePager;
import com.example.myone.Mapper.ArticleMapper;
import com.example.myone.R;
import com.example.myone.Util.ClickUtils;
import com.example.myone.Util.ImageUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

@RequiresApi(api = Build.VERSION_CODES.O)
public class HomeArticleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public List<Article> articleList;
    public Context context;
    private OnItemClickListener onItemClickListener;

    @Override
    public int getItemViewType(int position) {
        return HomePager.isLinear ? 1 : 2;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void updateData(List<Article> list) {
        articleList.clear();
        articleList.addAll(list);
        notifyDataSetChanged();
    }


    @SuppressLint("NotifyDataSetChanged")
    public void setLayoutType(String time) {
        if (HomePager.isLinear) {
            if (time == null || time.length() == 0) {
                time = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            }
            articleList = ArticleMapper.getArticleByTime(context, time);
        } else {
            articleList = ArticleMapper.getOneArticlePerDay(context);
        }
        notifyDataSetChanged();
    }

    public HomeArticleAdapter(Context context, List<Article> articleList) {
        this.context = context;
        this.articleList = articleList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view;
        if (HomePager.isLinear) {
            view = inflater.inflate(R.layout.home_pager_article_item, parent, false);
            return new LinearViewHolder(view);
        } else {
            view = inflater.inflate(R.layout.home_pager_article_time, parent, false);
            return new GridViewHolder(view);
        }
    }

    @SuppressLint("ResourceAsColor")
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Article article = articleList.get(position);
        if (holder instanceof LinearViewHolder) {
            System.out.println("==>onBindViewHolder LinearViewHolder");
            LinearViewHolder linearViewHolder = (LinearViewHolder) holder;
            linearViewHolder.category.setText(String.format("- %s -", article.category));
            linearViewHolder.title.setText(article.title);
            linearViewHolder.author.setText(String.format("文 / %s", article.author));
            linearViewHolder.content.setText(article.introduction);
            // 底部
            User authorUser = UserMapper.getUserById(context, article.authorId);
            linearViewHolder.authorDown.setText(article.author);
            ImageUtils.setImageByBase64Str(context, linearViewHolder.avatar, Objects.requireNonNull(authorUser).avatar);
            // 设置点赞按钮和计数器
            ClickUtils.setupLikeButton(context, article, linearViewHolder.likeButton, linearViewHolder.likeCount);
            // 设置分享按钮
            ClickUtils.setupBookButton(context, linearViewHolder.bookButton);
            ImageUtils.setImageByBase64Str(context, linearViewHolder.image, article.cover);
            // 设置点击事件
            ClickUtils.setClickToOtherActivityWithIntExtra
                    (context, AuthorActivity.class, "author_id", article.authorId, linearViewHolder.avatar, linearViewHolder.author);
            ClickUtils.setClickToOtherActivityWithIntExtra
                    (context, ArticleActivity.class, "article_id", article.id, linearViewHolder.relativeLayout);
        }
        else if (holder instanceof GridViewHolder) {
            System.out.println("==>onBindViewHolder GridViewHolder");
            GridViewHolder gridViewHolder = (GridViewHolder) holder;
            gridViewHolder.time.setText(article.createTime);
            ImageUtils.setImageByBase64Str(context, gridViewHolder.image, article.cover);
            // 点击触发onItemClick函数
            gridViewHolder.linearLayout.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(article.createTime);
                }
            });
        }
    }

    // 监听事件点击，传递给pager调用
    // 点击事件监听器接口
    public interface OnItemClickListener {
        void onItemClick(String createTime);
    }
    // 设置点击事件监听器的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }


    @Override
    public int getItemCount() {
        return articleList.size();
    }

    public static class LinearViewHolder extends RecyclerView.ViewHolder {
        TextView category, title, author, content, likeCount, authorDown;
        ImageView image, likeButton, bookButton, avatar;
        RelativeLayout relativeLayout;

        public LinearViewHolder(@NonNull View itemView) {
            super(itemView);
            category = itemView.findViewById(R.id.category);
            title = itemView.findViewById(R.id.title);
            author = itemView.findViewById(R.id.author);
            content = itemView.findViewById(R.id.content);
            likeCount = itemView.findViewById(R.id.like_count);
            likeButton = itemView.findViewById(R.id.like_button);
            bookButton = itemView.findViewById(R.id.book_button);
            image = itemView.findViewById(R.id.cover);
            authorDown = itemView.findViewById(R.id.author_down);
            avatar = itemView.findViewById(R.id.avatar);
            relativeLayout = itemView.findViewById(R.id.articleLayout);
        }
    }

    // 网格布局的视图
    public static class GridViewHolder extends RecyclerView.ViewHolder {
        TextView time;
        ImageView image;
        LinearLayout linearLayout;

        public GridViewHolder(@NonNull View itemView) {
            super(itemView);
            time = itemView.findViewById(R.id.time);
            image = itemView.findViewById(R.id.cover);
            linearLayout = itemView.findViewById(R.id.article_time_box);
        }
    }
}
